package com.shilox.android.app.logitnow.data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.shilox.android.app.logitnow.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LogMessageAdapter extends ArrayAdapter<LogMessage> {

	private static final int VERY_SAD_COLOR = Color.argb(255, 163, 139, 138);
	private static final int SAD_COLOR = Color.argb(255, 201, 211, 214);
	private static final int NORMAL_COLOR = Color.argb(255, 191, 230, 164);
	private static final int HAPPY_COLOR = Color.argb(255, 230, 140, 141);
	private static final int VERY_HAPPY_COLOR = Color.argb(255, 255, 54, 70);

	private static final int TYPE_SECTION_HEADER = 0;
	private static final int TYPE_LIST_ITEM = 1;

	// declaring our ArrayList of items
	private ArrayList<LogMessage> logMessages;

	/*
	 * here we must override the constructor for ArrayAdapter the only variable
	 * we care about now is ArrayList<Item> objects, because it is the list of
	 * objects we want to display.
	 */
	public LogMessageAdapter(Context context, int textViewResourceId,
			ArrayList<LogMessage> objects) {
		super(context, textViewResourceId, objects);
		this.logMessages = objects;
	}

	public void setList(ArrayList<LogMessage> lista) {
		logMessages = lista;
	}

	/*
	 * we are overriding the getView method here - this is what defines how each
	 * list item will look.
	 */
	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// assign the view we are converting to a local variable
		View v = convertView;
		LayoutInflater inflater = null;
		// first check to see if the view is null. if so, we have to inflate it.
		// to inflate it basically means to render, or show, the view.
		if (v == null) {
			inflater = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.row, null);
		}

		/*
		 * Recall that the variable position is sent in as an argument to this
		 * method. The variable simply refers to the position of the current
		 * object in the list. (The ArrayAdapter iterates through the list we
		 * sent it)
		 * 
		 * Therefore, i refers to the current Item object.
		 */
		LogMessage i = logMessages.get(position);

		if (i != null) {
			if (i.isHeader()) {
				if (inflater != null) {
					v = inflater.inflate(R.layout.section_header, null);
				}
				TextView list_header_title = (TextView) v
						.findViewById(R.id.list_header_title);
				Calendar c = Calendar.getInstance();
				c.setTime(i.getEntryDate());
				SimpleDateFormat sdf = new java.text.SimpleDateFormat(
						"dd/MM/yyyy");
				String fecha = sdf.format(i.getEntryDate());
				list_header_title.setText(fecha);
			} else {
				if (inflater != null) {
					v = inflater.inflate(R.layout.row, null);
				}
				// This is how you obtain a reference to the TextViews.
				// These TextViews are created in the XML files we defined.

				TextView logMessageText = (TextView) v
						.findViewById(R.id.logMessageText);
				TextView logMessageDate = (TextView) v
						.findViewById(R.id.logMessageDate);
				ImageView logMessageMood = (ImageView) v
						.findViewById(R.id.logMessageMood);

				LinearLayout ll = (LinearLayout) v.findViewById(R.id.feelColor);

				// check to see if each individual textview is null.
				// if not, assign some text!
				if (logMessageDate != null) {
					SimpleDateFormat sdf = new java.text.SimpleDateFormat(
							"HH:mm");
					String fecha = sdf.format(i.getEntryDate());
					logMessageDate.setText(fecha);
				}
				if (logMessageText != null) {
					logMessageText.setText(i.getTexto());
				}
				if (logMessageMood != null) {
					switch (i.getMood()) {
					case 1:
						logMessageMood.setImageResource(R.drawable.verysad_on);
						ll.setBackgroundColor(VERY_SAD_COLOR);
						break;
					case 2:
						logMessageMood.setImageResource(R.drawable.sad_on);
						ll.setBackgroundColor(SAD_COLOR);
						break;
					case 3:
						logMessageMood.setImageResource(R.drawable.normal_on);
						ll.setBackgroundColor(NORMAL_COLOR);
						break;
					case 4:
						logMessageMood.setImageResource(R.drawable.happy_on);
						ll.setBackgroundColor(HAPPY_COLOR);
						break;
					case 5:
						logMessageMood
								.setImageResource(R.drawable.veryhappy_on);
						ll.setBackgroundColor(VERY_HAPPY_COLOR);
						break;
					}
				}
			}
		}
		// the view must be returned to our activity
		return v;

	}

	@Override
	public LogMessage getItem(int position) {
		return logMessages.get(position);
	}

	@Override
	public int getCount() {
		return logMessages.size();
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		LogMessage item = logMessages.get(position);
		if (item.isHeader()) {
			return TYPE_SECTION_HEADER;
		} else {
			return TYPE_LIST_ITEM;
		}
	}

}
